package cn.ante.basic.controller;

import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.service.IVerifyCodeService;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.vo.BinderVo;
import cn.ante.basic.vo.EmailRegisterDto;
import cn.ante.basic.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.graph(key);
            return AjaxResult.me().setResultObj(base64Str);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }


    /**
    * @Description: 定义发送手机验证码的接口
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/6 12:38
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterVo vo){
        try {
            if ("phone".equals(vo.getRegisterType())){
                verifyCodeService.smsCode(vo);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }
    /**
    * @Description: 邮箱注册验证码
    * @Author: Nai
    * @Version: 1.0
    * @Date:
    */
    @PostMapping("/emailCode")
    public AjaxResult sendmailCode(@RequestBody RegisterVo vo) {
        try {
            if ("email".equals(vo.getRegisterType())){
                verifyCodeService.sendmailCode(vo);
            }
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    @PostMapping("/bindersmsCode")
    public AjaxResult binderSmsCode(@RequestBody BinderVo binderVo){
        try {
            verifyCodeService.binderSmsCode(binderVo);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }
}
